const router = require('koa-router')()
const mysql = require('../node_modules/mysql');
const fs = require('fs');
const path = require('path');
const db = require('../config/db');
const { title } = require('process');


router.get('/', async (ctx, next) => {
  let sqlSelect = 'select * from blog';
  
  console.log(db)

  let result = await db.query(sqlSelect,[])
  console.log(result);

  let chapterList = result;
  await ctx.render('chapterList',{
    chapterList:chapterList
  })
  
  
})
router.get('/detail/:id', async (ctx , next) => {
  let sqlSelectDetail = 'select * from blog where id=?';

  let result = await db.query(sqlSelectDetail,[ctx.params.id]);

  console.log(result[0]);
  

  await ctx.render('chapter.ejs',{
    chapter:result[0]
  })
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router
